<?php

	include 'csh.php';

	/**
	 * 查询语句
	 * @param String $dbName
	 * @param String $offset_num
	 * @param String $where
	 * 
	*/
	function action_search($pdo,$dbName,$data="",$offset_num="0"){
		//使用PDO中的方法执行SQL语句
		if(!$data){
			$sql = "SELECT * FROM $dbName LIMIT 10 OFFSET $offset_num;";
		}else{
			$where = 'WHERE '.implode(',',array_keys($data)).'='.implode("','",$data);
			// echo $where;
			$sql = "SELECT * FROM $dbName $where LIMIT 10 OFFSET $offset_num;";
		}
		$res = $pdo->query($sql);
		$arr = $res->fetchAll(PDO::FETCH_ASSOC);
		// foreach ($arr as $key => $value) {
		// 	if(isset($arr[$key]['password'])){
		// 		unset($arr[$key]['password']);
		// 	}
		// }
		$data = json_encode($arr,true);
		echo $data;
	}

	/**
	 * 插入语句
	 * @param String $dbName
	 * @param Array $data = ["name"=>"value"]
	 * 
	*/
	function action_insert($pdo,$dbName,$data){
		//使用PDO中的方法执行SQL语句
		isset($data['password']) ? $data['password'] = md5($data['password']) : '';
		$sql = "INSERT INTO ".$dbName." (".implode(',',array_keys($data)).") VALUES ('".implode("','",$data)."')";
		$return = $pdo->exec($sql);
		echo $return;
	}


	function action_delete($pdo,$dbName,$data){

		//  预处理
		//  发送预处理
		$sql = "DELETE FROM $dbName WHERE pro_class_id in (?);";
		$stmt = $pdo->prepare($sql);

		foreach ($data as $key => $value) {
			// 绑定参数
			// 先把每个参数保存到一个变量中
			$pro_class_id = $value['pro_class_id'];
			//再使用bindParam方法绑定参数变量
			$stmt->bindParam(1,$pro_class_id);    //1代表第一个数据占位符

			// 执行预处理语句
			$res = $stmt->execute();
			echo($res);
		}
	}


	function action_upData($pdo,$dbName,$data){
		//使用PDO中的方法执行SQL语句
		$strSql = '';
		foreach ($data as $key => $value) {
			$strSql .= ", $key='$value'";
		}
		$where = "WHERE pro_class_id = ".$data['pro_class_id'];
		$strSql = substr($strSql, 1);
		$sql = "UPDATE $dbName SET $strSql $where";
		$return = $pdo->exec($sql);
		echo($return);
	}	

?>